#ifndef __LIBS_ATOMIC_H__
#define __LIBS_ATOMIC_H__

/* Temporary fix due to cross referencing */
#include <sync.h>

/* Atomic operations that C can't guarantee us. Useful for resource counting etc.. */

typedef struct {
    volatile int counter;
} atomic_t;

static inline int atomic_read(const atomic_t *v) __attribute__((always_inline));
static inline void atomic_set(atomic_t *v, int i) __attribute__((always_inline));
//static inline void atomic_add(atomic_t *v, int i) __attribute__((always_inline));
//static inline void atomic_sub(atomic_t *v, int i) __attribute__((always_inline));
//static inline bool atomic_sub_test_zero(atomic_t *v, int i) __attribute__((always_inline));
//static inline void atomic_inc(atomic_t *v) __attribute__((always_inline));
//static inline void atomic_dec(atomic_t *v) __attribute__((always_inline));
//static inline bool atomic_inc_test_zero(atomic_t *v) __attribute__((always_inline));
//static inline bool atomic_dec_test_zero(atomic_t *v) __attribute__((always_inline));
static inline int atomic_add_return(atomic_t *v, int i) __attribute__((always_inline));
static inline int atomic_sub_return(atomic_t *v, int i) __attribute__((always_inline));

/* *
 * atomic_read - read atomic variable
 * @v:  pointer of type atomic_t
 *
 * Atomically reads the value of @v.
 * */
static inline int
atomic_read(const atomic_t *v) {
    return v->counter;
}

/* *
 * atomic_set - set atomic variable
 * @v:  pointer of type atomic_t
 * @i:  required value
 *
 * Atomically sets the value of @v to @i.
 * */
static inline void
atomic_set(atomic_t *v, int i) {
    v->counter = i;
}

/* *
 * atomic_add - add integer to atomic variable
 * @v:  pointer of type atomic_t
 * @i:  integer value to add
 *
 * Atomically adds @i to @v.
 * */
#define atomic_add(v, i)        (void) atomic_add_return(v, i)


/* *
 * atomic_sub - subtract integer from atomic variable
 * @v:  pointer of type atomic_t
 * @i:  integer value to subtract
 *
 * Atomically subtracts @i from @v.
 * */
#define atomic_sub(v, i)        (void) atomic_sub_return(v, i)

/* *
 * atomic_sub_test_zero - subtract value from variable and test result
 * @v:  pointer of type atomic_t
 * @i:  integer value to subtract
 *
 * Atomically subtracts @i from @v and
 * returns true if the result is zero, or false for all other cases.
 * */
#define atomic_sub_test_zero(v, i)  (atomic_sub_return(v, i) == 0)

/* *
 * atomic_inc - increment atomic variable
 * @v:  pointer of type atomic_t
 *
 * Atomically increments @v by 1.
 * */
#define atomic_inc(v)           atomic_add(v, 1)


/* *
 * atomic_dec - decrement atomic variable
 * @v:  pointer of type atomic_t
 *
 * Atomically decrements @v by 1.
 * */
#define atomic_dec(v)           atomic_sub(v, 1)

/* *
 * atomic_inc_test_zero - increment and test
 * @v:  pointer of type atomic_t
 *
 * Atomically increments @v by 1 and
 * returns true if the result is zero, or false for all other cases.
 * */
#define atomic_inc_test_zero(v)  (atomic_add_return(v, 1) == 0)

/* *
 * atomic_dec_test_zero - decrement and test
 * @v:  pointer of type atomic_t
 *
 * Atomically decrements @v by 1 and
 * returns true if the result is 0, or false for all other cases.
 * */
#define atomic_dec_test_zero(v)  (atomic_sub_return(v, 1) == 0)

/* *
 * atomic_add_return - add integer and return
 * @i:  integer value to add
 * @v:  pointer of type atomic_t
 *
 * Atomically adds @i to @v and returns @i + @v
 * For ARMvX < ARMv6
 * */
static inline int 
atomic_add_return(atomic_t *v, int i)
{
	uint32_t flags;
	int val;
	local_intr_save(flags);
	val = v->counter;
	v->counter = val += i;
	local_intr_restore(flags);
	return val;
}

/* *
 * atomic_sub_return - subtract integer and return
 * @v:  pointer of type atomic_t
 * @i:  integer value to subtract
 *
 * Atomically subtracts @i from @v and returns @v - @i
 * For ARMvX < ARMv6
 * */
static inline int 
atomic_sub_return(atomic_t *v, int i)
{
	uint32_t flags;
	int val;
	local_intr_save(flags);
	val = v->counter;
	v->counter = val -= i;
	local_intr_restore(flags);
	return val;
}

static inline void set_bit(int nr, volatile uint32_t *addr) __attribute__((always_inline));
static inline void clear_bit(int nr, volatile uint32_t *addr) __attribute__((always_inline));
static inline void change_bit(int nr, volatile uint32_t *addr) __attribute__((always_inline));
static inline bool test_and_set_bit(int nr, volatile uint32_t *addr) __attribute__((always_inline));
static inline bool test_and_clear_bit(int nr, volatile uint32_t *addr) __attribute__((always_inline));
static inline bool test_and_change_bit(int nr, volatile uint32_t *addr) __attribute__((always_inline));
static inline bool test_bit(int nr, volatile uint32_t *addr) __attribute__((always_inline));

/* *
 * set_bit - Atomically set a bit in memory
 * @nr:     the bit to set
 * @addr:   the address to start counting from
 *
 * Note that @nr may be almost arbitrarily large; this function is not
 * restricted to acting on a single-word quantity.
 * */
static inline void 
set_bit(int nr, volatile uint32_t *addr)
{
	uint32_t flags;
	uint32_t mask = 1UL << (nr & 31);

	addr += nr >> 5;

	local_intr_save(flags);
	*addr |= mask;
	local_intr_restore(flags);
}

/* *
 * clear_bit - Atomically clears a bit in memory
 * @nr:     the bit to clear
 * @addr:   the address to start counting from
 * */
static inline void 
clear_bit(int nr, volatile uint32_t *addr)
{
	uint32_t flags;
	uint32_t mask = 1UL << (nr & 31);

	addr += nr >> 5;

	local_intr_save(flags);
	*addr &= ~mask;
	local_intr_restore(flags);
}

/* *
 * change_bit - Atomically toggle a bit in memory
 * @nr:     the bit to change
 * @addr:   the address to start counting from
 * */
static inline void 
change_bit(int nr, volatile uint32_t *addr)
{
	uint32_t flags;
	uint32_t mask = 1UL << (nr & 31);

	addr += nr >> 5;

	local_intr_save(flags);
	*addr ^= mask;
	local_intr_restore(flags);
}

/* *
 * test_and_set_bit - Atomically set a bit and return its old value
 * @nr:     the bit to set
 * @addr:   the address to count from
 * */
static inline bool
test_and_set_bit(int nr, volatile uint32_t  *addr)
{
	uint32_t flags;
	uint32_t res;
	uint32_t mask = 1UL << (nr & 31);

	addr += nr >> 5;

	local_intr_save(flags);
	res = *addr;
	*addr = res | mask;
	local_intr_restore(flags);

	return (res & mask) != 0;
}

/* *
 * test_and_clear_bit - Atomically clear a bit and return its old value
 * @nr:     the bit to clear
 * @addr:   the address to count from
 * */
static inline bool
test_and_clear_bit(int nr, volatile uint32_t *addr)
{
	uint32_t flags;
	uint32_t res;
	uint32_t mask = 1UL << (nr & 31);

	addr += nr >> 5;

	local_intr_save(flags);
	res = *addr;
	*addr = res & ~mask;
	local_intr_restore(flags);

	return (res & mask) != 0;
}


/* *
 * test_and_change_bit - Atomically change a bit and return its old value
 * @nr:     the bit to change
 * @addr:   the address to count from
 * */
static inline bool
test_and_change_bit(int nr, volatile uint32_t *addr)
{
	uint32_t flags;
	uint32_t res;
	uint32_t mask = 1UL << (nr & 31);

	addr += nr >> 5;

	local_intr_save(flags);
	res = *addr;
	*addr = res ^ mask;
	local_intr_restore(flags);

	return (res & mask) != 0;
}

/* *
 * test_bit - Determine whether a bit is set
 * @nr:     the bit to test
 * @addr:   the address to count from
 * */
static inline bool
test_bit(int nr, volatile uint32_t *addr) {
	uint32_t flags;
	uint32_t res;
	uint32_t mask = 1UL << (nr & 31);

	addr += nr >> 5;

	local_intr_save(flags);
	res = *addr;
	local_intr_restore(flags);

	return (res & mask) != 0;
}

#include <assert.h>

typedef volatile uint32_t lock_t; // type has been modified

static inline void
lock_init(lock_t *lock) {
    *lock = 0;
}

static inline bool
try_lock(lock_t *lock) {
    return !test_and_set_bit(0, lock);
}

static inline void
lock(lock_t *lock) {
    // deadlock
    while (!try_lock(lock));
}

static inline void
unlock(lock_t *lock) {
    if (!test_and_clear_bit(0, lock)) {
        panic("Unlock failed.\n");
    }
}

#endif /* !__LIBS_ATOMIC_H__ */

